package Study.demo12;

import java.sql.*;

public class jdbcDemo {
    public static void connectData(String Url,String UserName,String Password){

        //加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //与数据库连接
            Connection conn=DriverManager.getConnection(Url,UserName,Password);
            //获取statement对象
            Statement statement=conn.createStatement();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
    public static void selectData(Statement statement,String sql){
        try {
            ResultSet result=statement.executeQuery(sql);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) {
        //1.加载驱动
        try {
            //本身找不到驱动包，自行创建lib导入驱动包
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取数据库连接
            Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/aaa","root","");
       //获取statemen对象，conection接口中的对象用于执行静态的SQL语句
             Statement statement= conn.createStatement();
             //提供了增删改查方法
            //Result用于将查询结构放在Resultset集中
            //executeUpdate 用于执行update的操作
           ResultSet  result= statement.executeQuery("select * from dept");
            /*插入新的数据
            statement.executeUpdate("insert into dept(deptno,dname,loc) values(55,'SADAWE','DADA')");
            正确返回1，错误返回0；
            int isRight=statement.executeUpdate("insert into dept(deptno,dname,loc) values (256,'ASkkA','SSKY')");
            更新数据
            statement.executeUpdate("update dept set loc='HHDHD' where dname='SADAWE'");
            System.out.println(isRight);
                result.close();
            遍历结果集
          遍历结果集*/
            /*删除数据
           int isright= statement.executeUpdate("delete from dept where loc='UKKY'");
            System.out.println(isright);
             */
            //创建和删除表数据
            //statement.executeUpdate("create table users(id int,username varchar(10))");
            //删除表
            //statement.executeUpdate("drop table users");
            //修改表结构
           /* while (result.next()) {
            //获取当前每一列的值,利用循环遍历每一列
                int deptno=result.getInt("deptno");
                String deptname=result.getString("dname");
                String deptaddress=result.getString("loc");
                System.out.println(deptno+" "+deptname+" "+deptaddress);
            }
            //资源的释放，按照顺序释放
            result.close();
            statement.close();

            */

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

}
